// 根级别的 action
// 封装复杂的 mutations

import * as types from './mutations-type.js'
import utils from '../assets/js/utils'
import store from '../assets/js/store'

function findIndex(list, song) {
    return list.findIndex((item) => {
        return item.id === song.id
    })
}


export const selectPlay = function({ commit, state }, { list, index }) {
    commit(types.SET_PLAYING_STATE, true)
    commit(types.SET_FULL_SCREEN, true)

    if (state.mode === 2) {
        let randomList = myArray.shuffle(list)
        commit(types.SET_PLAYLIST, randomList)
        index = findIndex(randomList, list[index])
    } else {
        commit(types.SET_PLAYLIST, list)
    }
    commit(types.SET_SEQUENCE_LIST, list)
    commit(types.SET_CURRENT_INDEX, index)
}


export const randomPlay = function({ commit }, { list }) {
    commit(types.SET_MODE, 2)
    commit(types.SET_PLAYING_STATE, true)
    commit(types.SET_FULL_SCREEN, true)
    commit(types.SET_PLAYLIST, myArray.shuffle(list))
    commit(types.SET_SEQUENCE_LIST, list)
    commit(types.SET_CURRENT_INDEX, 0)
}


export const insertSong = function({ commit, state }, song) {
    let playlist = state.playlist.slice(0)
    let sequenceList = state.sequenceList.slice(0)
    let currentIndex = state.currentIndex


    let currentSong = playlist[currentIndex]


    let fpIndex = findIndex(playlist, song)


    currentIndex++
    playlist.splice(currentIndex, 0, song)


    if (fpIndex > -1) {
        if (currentIndex > fpIndex) {
            playlist.splice(fpIndex, 1)
            currentIndex--
        } else {
            playlist.splice(fpIndex + 1, 1)
        }
    }

    let currentSIndex = findIndex(sequenceList, currentSong) + 1


    let fsIndex = findIndex(sequenceList, song)


    sequenceList.splice(currentSIndex, 0, song)


    if (fsIndex > -1) {
        if (currentSIndex > fsIndex) {
            sequenceList.splice(fsIndex, 1)
        } else {
            sequenceList.splice(fsIndex + 1, 1)
        }
    }

    commit(types.SET_PLAYLIST, playlist)
    commit(types.SET_SEQUENCE_LIST, sequenceList)
    commit(types.SET_CURRENT_INDEX, currentIndex)
    commit(types.SET_PLAYING_STATE, true)
    commit(types.SET_FULL_SCREEN, true)
}


export const deleteSong = function({ commit, state }, song) {
    let playlist = state.playlist.slice(0)
    let sequenceList = state.sequenceList.slice(0)
    let currentIndex = state.currentIndex


    let pIndex = findIndex(playlist, song)
    playlist.splice(pIndex, 1)

    // sequenceList 中的位置
    let sIndex = findIndex(sequenceList, song)
    sequenceList.splice(sIndex, 1)

    if (currentIndex > pIndex || currentIndex === playlist.length) {
        currentIndex--
    }

    commit(types.SET_PLAYLIST, playlist)
    commit(types.SET_SEQUENCE_LIST, sequenceList)
    commit(types.SET_CURRENT_INDEX, currentIndex)

    let playingState = playlist.length > 0
    commit(types.SET_PLAYING_STATE, playingState)
}

/**
 * 保存搜索历史
 * @param  {[type]} query          搜索关键词
 */
export const saveHistory = function({ commit }, query) {
    commit(types.SET_SEARCHHISTORY, localSave(query))
}

/**
 * 删除单个搜索历史
 * @param  {[type]} query          搜索关键词
 */
export const delHistory = function({ commit }, query) {
    commit(types.SET_SEARCHHISTORY, localDel(query))
}

/**
 * 删除全部搜索历史
 * @param  {[type]} query          搜索关键词
 */
export const clearHistory = function({ commit }) {
    commit(types.SET_SEARCHHISTORY, localClear())
}


export const deleteSongList = function({ commit, state }, song) {
    commit(types.SET_PLAYLIST, [])
    commit(types.SET_SEQUENCE_LIST, [])
    commit(types.SET_CURRENT_INDEX, -1)
    commit(types.SET_PLAYING_STATE, false)
}


export const saveplayHistory = function({ commit }, song) {
    commit(types.SET_PLAYHISTORY, savePlay(song))
}

// 我的收藏
export const savefavoriteList = function({ commit }, song) {
    commit(types.SET_FAVORITE_LIST, saveFavorite(song))
}

export const delfavoriteList = function({ commit }, song) {
    commit(types.SET_FAVORITE_LIST, delFavorite(song))
}